package com.cyt.web.controller.work.kwmanagement;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.util.EasyPoiUtil;
import com.cyt.work.kwmanagement.dao.KwStudentMessageDao;
import com.cyt.work.kwmanagement.model.vo.KwKcSettingVo;
import com.cyt.work.kwmanagement.model.vo.KwKcStudnetScoreVo;
import com.cyt.work.kwmanagement.model.vo.KwStudentMessageExportVo;
import com.cyt.work.kwmanagement.model.vo.KwStudentMessageVo;
import com.cyt.work.kwmanagement.service.KwKcSettingService;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.BaseController;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 考场设置表-考场设置表控制层
 *
 * @author han jun
 * @date 2019-11-26 11:22:27
 */
@RestController
public class KwKcSettingController extends BaseController {

    @Resource
    private KwKcSettingService kwKcSettingService;

    @Resource
    private KwStudentMessageDao kwStudentMessageDao;

    /**
     * 统一跳转
     *
     * @param model 数据模型
     * @return Model
     * @author han jun
     * @date 2019-11-26 11:22:27
     */
    @RequestMapping("/work/kwmanagement/kwkcsetting/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 分页查询
     *
     * @param kwKcSettingVo 接受参数对象
     * @param page          当前页面
     * @param limit         页面数量
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:22:27
     */
    @BaseLog(module = "考场设置表", classz = "考场设置表", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/kwmanagement/kwkcsetting/page", method = RequestMethod.POST)
    public ReturnResult page(KwKcSettingVo kwKcSettingVo, int page, int limit) {
        return kwKcSettingService.page(kwKcSettingVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:22:27
     */
    @BaseLog(module = "考场设置表", classz = "考场设置表", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/kwmanagement/kwkcsetting/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id) {
        ModelAndView view = new ModelAndView("/work/kwmanagement/kwkcsetting/kwKcSettingEdit");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(kwKcSettingService.selectByPrimaryKey(id));
        }
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request       请求对象
     * @param kwKcSettingVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:22:27
     */
    @BaseLog(module = "考场设置表", classz = "考场设置表", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/kwmanagement/kwkcsetting/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, KwKcSettingVo kwKcSettingVo) {
        ReturnResult a = kwKcSettingService.addOrUpdate(kwKcSettingVo, acceptCurrentUser(request));
        return a;

    }

    /**
     * 根据条件逻辑删除
     *
     * @param request       请求对象
     * @param kwKcSettingVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:22:27
     */
    @BaseLog(module = "考场设置表", classz = "考场设置表", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/kwmanagement/kwkcsetting/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, KwKcSettingVo kwKcSettingVo) {
        return kwKcSettingService.remove(kwKcSettingVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request       请求对象
     * @param kwKcSettingVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:22:27
     */
    @BaseLog(module = "考场设置表", classz = "考场设置表", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/kwmanagement/kwkcsetting/delete", method = RequestMethod.POST)
    public ReturnResult delete(HttpServletRequest request, KwKcSettingVo kwKcSettingVo) {
        return kwKcSettingService.delete(kwKcSettingVo, acceptCurrentUser(request));
    }

    /**
     * 获取考场设置
     *
     * @param request       请求对象
     * @param kwKcSettingVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:22:27
     */
    @BaseLog(module = "考场设置表", classz = "考场设置表", methods = "获取考场设置", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/kwmanagement/kwkcsetting/findSettingOneVo", method = RequestMethod.POST)
    public ReturnResult findSettingOneVo(HttpServletRequest request, KwKcSettingVo kwKcSettingVo) {
        KwKcSettingVo kwKcSettingVo1 = kwKcSettingService.findOneVo(kwKcSettingVo);
        return new ReturnResult(kwKcSettingVo1);
    }

    /**
     * 导出考务的学生成绩模板
     *
     * @param response
     * @author hanjun
     * @date 2019/9/3 13:43
     */
    @RequestMapping(value = "/work/kwmanagement/kwkcsetting/exportStudentScore")
    public void exportStudentScore(HttpServletResponse response, KwKcSettingVo kwKcSettingVo) {
        //获取所有参加考试的学生
        KwStudentMessageVo kwStudentMessageVo = new KwStudentMessageVo();
        kwStudentMessageVo.setApplyId(kwKcSettingVo.getApplyId());
        kwStudentMessageVo.setKsId(kwKcSettingVo.getKsId());

        List<KwKcStudnetScoreVo> newList = new ArrayList<>();
        List<KwStudentMessageVo> list = kwStudentMessageDao.selectVoList(kwStudentMessageVo);
        for (KwStudentMessageVo kwStudentMessagevoNew : list) {
            newList.add(new KwKcStudnetScoreVo(kwStudentMessagevoNew.getUserNumber(),kwStudentMessagevoNew.getName(),"", "", "", "", "", "", "", "", ""));
        }
        // 导出操作
        EasyPoiUtil.exportExcel(newList, "学生成绩模板", "学生成绩模板", KwKcStudnetScoreVo.class, "学生成绩模板.xls", response);
    }
}

